package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;

/**
 * @description:
 * @author: 付益民
 * @create: 2020-09-26-20:15
 */
@Component
public class TxAdvice {
    @Autowired
    private DataSource dataSource;

    public void transactionManager(ProceedingJoinPoint pjp) throws Throwable {
        DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource);
        TransactionDefinition td = new DefaultTransactionDefinition();
        TransactionStatus ts = dstm.getTransaction(td);
        Object ret = pjp.proceed(pjp.getArgs());
        dstm.commit(ts);
    }
}
